
<template>
	<div class="app-contain" :style='{"minHeight":"100vh","padding":"0 18%","margin":"20px auto 60px","color":"#666","background":"#fff","width":"100%","fontSize":"14px","position":"relative","height":"100%"}'>
		<div class="bread_view">
			<el-breadcrumb separator="/" class="breadcrumb">
				<el-breadcrumb-item class="first_breadcrumb" :to="{ path: '/' }">首页</el-breadcrumb-item>
				<el-breadcrumb-item class="second_breadcrumb" v-for="(item,index) in breadList" :key="index">{{item.name}}</el-breadcrumb-item>
			</el-breadcrumb>
		</div>
		<el-form ref="formRef" :model="form" class="add_form" label-width="120px" :rules="rules">
			<el-row>
				<el-col :span="24">
					<el-form-item label="缴费单号" prop="jiaofeidanhao">
						<el-input class="list_inp" v-model="form.jiaofeidanhao" placeholder="请输入缴费单号" readonly></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="账号" prop="zhanghao">
						<el-input class="list_inp" v-model="form.zhanghao" placeholder="账号"
							 type="text" 							:readonly="!isAdd||disabledForm.zhanghao?true:false" />
					</el-form-item>
				</el-col>

				<el-col :span="24">
					<el-form-item label="姓名" prop="xingming">
						<el-input class="list_inp" v-model="form.xingming" placeholder="姓名"
							 type="text" 							:readonly="!isAdd||disabledForm.xingming?true:false" />
					</el-form-item>
				</el-col>

				<el-col :span="24">
					<el-form-item label="检查项目" prop="jianchaxiangmu">
						<el-input class="list_inp" v-model="form.jianchaxiangmu" placeholder="检查项目"
							 type="text" 							:readonly="!isAdd||disabledForm.jianchaxiangmu?true:false" />
					</el-form-item>
				</el-col>

				<el-col :span="24">
					<el-form-item label="药品名称" prop="yaopinmingcheng">
						<el-input class="list_inp" v-model="form.yaopinmingcheng" placeholder="药品名称"
							 type="text" 							:readonly="!isAdd||disabledForm.yaopinmingcheng?true:false" />
					</el-form-item>
				</el-col>

				<el-col :span="24">
					<el-form-item label="检查费用" prop="jianchafeiyong">
						<el-input class="list_inp" v-model.number="form.jianchafeiyong" placeholder="检查费用"
							 type="number" 							:readonly="!isAdd||disabledForm.jianchafeiyong?true:false" />
					</el-form-item>
				</el-col>

				<el-col :span="24">
					<el-form-item label="药品费用" prop="yaopinfeiyong">
						<el-input class="list_inp" v-model.number="form.yaopinfeiyong" placeholder="药品费用"
							 type="number" 							:readonly="!isAdd||disabledForm.yaopinfeiyong?true:false" />
					</el-form-item>
				</el-col>

				<el-col :span="24">
					<el-form-item label="总费用" prop="zongfeiyong">
						<el-input class="list_inp" v-model="zongfeiyong" placeholder="总费用" readonly></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="缴费日期" prop="jiaofeiriqi">
						<el-date-picker
							class="list_date"
							v-model="form.jiaofeiriqi"
							format="YYYY 年 MM 月 DD 日"
							value-format="YYYY-MM-DD"
							type="datetime"
							:readonly="!isAdd||disabledForm.jiaofeiriqi?true:false"
							placeholder="请选择缴费日期"
							style="width: 100%;" />
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="员工账号" prop="yuangongzhanghao">
						<el-input class="list_inp" v-model="form.yuangongzhanghao" placeholder="员工账号"
							 type="text" 							:readonly="!isAdd||disabledForm.yuangongzhanghao?true:false" />
					</el-form-item>
				</el-col>

				<el-col :span="24">
					<el-form-item label="员工姓名" prop="yuangongxingming">
						<el-input class="list_inp" v-model="form.yuangongxingming" placeholder="员工姓名"
							 type="text" 							:readonly="!isAdd||disabledForm.yuangongxingming?true:false" />
					</el-form-item>
				</el-col>

			</el-row>
			<div class="formModel_btn_box">
				<el-button class="formModel_cancel" @click="backClick">取消</el-button>
				<el-button class="formModel_confirm" @click="save"
					type="success"
					>
					保存
				</el-button>
			</div>
		</el-form>
	</div>
</template>
<script setup>
	import {
		ref,
		getCurrentInstance,
		watch,
		onUnmounted,
		onMounted,
		nextTick,
		computed
	} from 'vue';
	import {
		useRoute,
		useRouter
	} from 'vue-router';
	const context = getCurrentInstance()?.appContext.config.globalProperties;
	const route = useRoute()
	const router = useRouter()
	//基础信息
	const tableName = 'menzhenshoufei'
	const formName = '门诊收费'
	//基础信息
	const breadList = ref([{
		name: formName
	}])
	//获取唯一标识
	const getUUID =()=> {
      return new Date().getTime();
    }
	//form表单
	const form = ref({
		jiaofeidanhao: getUUID(),
		zhanghao: '',
		xingming: '',
		jianchaxiangmu: '',
		yaopinmingcheng: '',
		jianchafeiyong: 0,
		yaopinfeiyong: 0,
		zongfeiyong: 0,
		jiaofeiriqi: '',
		yuangongzhanghao: '',
		yuangongxingming: '',
		ispay: '未支付',
	})
	const formRef = ref(null)
	const id = ref(0)
	const type = ref('')
	const disabledForm = ref({
		jiaofeidanhao : false,
		zhanghao : false,
		xingming : false,
		jianchaxiangmu : false,
		yaopinmingcheng : false,
		jianchafeiyong : false,
		yaopinfeiyong : false,
		zongfeiyong : false,
		jiaofeiriqi : false,
		yuangongzhanghao : false,
		yuangongxingming : false,
		ispay : false,
	})
	const isAdd = ref(false)
	//表单验证
	//匹配整数
	const validateIntNumber = (rule, value, callback) => {
		if (!value) {
			callback();
		} else if (!context?.$toolUtil.isIntNumer(value)) {
			callback(new Error("请输入整数"));
		} else {
			callback();
		}
	}
	//匹配数字
	const validateNumber = (rule, value, callback) => {
		if(!value){
			callback();
		} else if (!context?.$toolUtil.isNumber(value)) {
			callback(new Error("请输入数字"));
		} else {
			callback();
		}
	}
	//匹配手机号码
	const validateMobile = (rule, value, callback) => {
		if(!value){
			callback();
		} else if (!context?.$toolUtil.isMobile(value)) {
			callback(new Error("请输入正确的手机号码"));
		} else {
			callback();
		}
	}
	//匹配电话号码
	const validatePhone = (rule, value, callback) => {
		if(!value){
			callback();
		} else if (!context?.$toolUtil.isPhone(value)) {
			callback(new Error("请输入正确的电话号码"));
		} else {
			callback();
		}
	}
	//匹配邮箱
	const validateEmail = (rule, value, callback) => {
		if(!value){
			callback();
		} else if (!context?.$toolUtil.isEmail(value)) {
			callback(new Error("请输入正确的邮箱地址"));
		} else {
			callback();
		}
	}
	//匹配身份证
	const validateIdCard = (rule, value, callback) => {
		if(!value){
			callback();
		} else if (!context?.$toolUtil.checkIdCard(value)) {
			callback(new Error("请输入正确的身份证号码"));
		} else {
			callback();
		}
	}
	//匹配网站地址
	const validateUrl = (rule, value, callback) => {
		if(!value){
			callback();
		} else if (!context?.$toolUtil.isURL(value)) {
			callback(new Error("请输入正确的URL地址"));
		} else {
			callback();
		}
	}
	const rules = ref({
		jiaofeidanhao: [
		],
		zhanghao: [
		],
		xingming: [
		],
		jianchaxiangmu: [
		],
		yaopinmingcheng: [
		],
		jianchafeiyong: [
			{ validator: validateNumber, trigger: 'blur' },
		],
		yaopinfeiyong: [
			{ validator: validateNumber, trigger: 'blur' },
		],
		zongfeiyong: [
			{ validator: validateNumber, trigger: 'blur' },
		],
		jiaofeiriqi: [
		],
		yuangongzhanghao: [
		],
		yuangongxingming: [
		],
		ispay: [
		],
	})
	const zongfeiyong =computed(()=>{
		let c = form.value
		let a = Number(c.jianchafeiyong)+Number(c.yaopinfeiyong)
		form.value.zongfeiyong = a?a.toFixed(2):0
		return a?a.toFixed(2):0
	})
	//methods

	//methods
	//获取info
	const getInfo = ()=>{
		context?.$http({
			url: `${tableName}/info/${id.value}`,
			method: 'get'
		}).then(res => {
			let reg=new RegExp('../../../file','g')
			form.value = res.data.data
		})
	}
	const crossRow = ref('')
	const crossTable = ref('')
	const crossTips = ref('')
	const crossColumnName = ref('')
	const crossColumnValue = ref('')
	//初始化
	const init = (formId=null,formType='add',formNames='',row=null,table=null,statusColumnName=null,tips=null,statusColumnValue=null) => {
			form.value.jiaofeiriqi = context?.$toolUtil.getCurDate()
		if(formId){
			id.value = formId
			type.value = formType
		}
		if(formType == 'add'){
			isAdd.value = true
		}else if(formType == 'info'){
			isAdd.value = false
			getInfo()
		}else if(formType == 'edit'){
			isAdd.value = true
			getInfo()
		}
		else if(formType == 'cross'){
			isAdd.value = true
			// getInfo()
			for(let x in row){
				if(x=='jiaofeidanhao'){
					form.value.jiaofeidanhao = row[x];
					disabledForm.value.jiaofeidanhao = true;
					continue;
				}
				if(x=='zhanghao'){
					form.value.zhanghao = row[x];
					disabledForm.value.zhanghao = true;
					continue;
				}
				if(x=='xingming'){
					form.value.xingming = row[x];
					disabledForm.value.xingming = true;
					continue;
				}
				if(x=='jianchaxiangmu'){
					form.value.jianchaxiangmu = row[x];
					disabledForm.value.jianchaxiangmu = true;
					continue;
				}
				if(x=='yaopinmingcheng'){
					form.value.yaopinmingcheng = row[x];
					disabledForm.value.yaopinmingcheng = true;
					continue;
				}
				if(x=='jianchafeiyong'){
					form.value.jianchafeiyong = row[x];
					disabledForm.value.jianchafeiyong = true;
					continue;
				}
				if(x=='yaopinfeiyong'){
					form.value.yaopinfeiyong = row[x];
					disabledForm.value.yaopinfeiyong = true;
					continue;
				}
				if(x=='zongfeiyong'){
					form.value.zongfeiyong = row[x];
					disabledForm.value.zongfeiyong = true;
					continue;
				}
				if(x=='jiaofeiriqi'){
					form.value.jiaofeiriqi = row[x];
					disabledForm.value.jiaofeiriqi = true;
					continue;
				}
				if(x=='yuangongzhanghao'){
					form.value.yuangongzhanghao = row[x];
					disabledForm.value.yuangongzhanghao = true;
					continue;
				}
				if(x=='yuangongxingming'){
					form.value.yuangongxingming = row[x];
					disabledForm.value.yuangongxingming = true;
					continue;
				}
			}
			if(row){
				crossRow.value = row
			}
			if(table){
				crossTable.value = table
			}
			if(tips){
				crossTips.value = tips
			}
			if(statusColumnName){
				crossColumnName.value = statusColumnName
			}
			if(statusColumnValue){
				crossColumnValue.value = statusColumnValue
			}
			form.value.ispay='未支付'
		}
		context?.$http({
			url: `${context?.$toolUtil.storageGet('frontSessionTable')}/session`,
			method: 'get'
		}).then(res => {
			var json = res.data.data
			if(json.hasOwnProperty('yuangongzhanghao') && context?.$toolUtil.storageGet("frontRole")!="管理员"){
				form.value.yuangongzhanghao = json.yuangongzhanghao
				disabledForm.value.yuangongzhanghao = true;
			}
			if(json.hasOwnProperty('yuangongxingming') && context?.$toolUtil.storageGet("frontRole")!="管理员"){
				form.value.yuangongxingming = json.yuangongxingming
				disabledForm.value.yuangongxingming = true;
			}
		})
	}
	//初始化
	//取消
	const backClick = () => {
		history.back()
	}
	//提交
	const save=()=>{
		var table = crossTable.value
		var objcross = JSON.parse(JSON.stringify(crossRow.value))
		let crossUserId = ''
		let crossRefId = ''
		let crossOptNum = ''
		if(type.value == 'cross'){
			if(crossColumnName.value!=''){
				if(!crossColumnName.value.startsWith('[')){
					for(let o in objcross){
						if(o == crossColumnName.value){
							objcross[o] = crossColumnValue.value
						}
					}
					//修改跨表数据
					changeCrossData(objcross)
				}else{
					crossUserId = context?.$toolUtil.storageGet('userid')
					crossRefId = objcross['id']
					crossOptNum = crossColumnName.value.replace(/\[/,"").replace(/\]/,"")
				}
			}
		}
		formRef.value.validate((valid)=>{
			if(valid){
				if(crossUserId&&crossRefId){
					form.value.crossuserid = crossUserId
					form.value.crossrefid = crossRefId
					let params = {
						page: 1,
						limit: 1000, 
						crossuserid:form.value.crossuserid,
						crossrefid:form.value.crossrefid,
					}
					context?.$http({
						url: `${tableName}/page`,
						method: 'get', 
						params: params 
					}).then(res=>{
						if(res.data.data.total>=crossOptNum){
							context?.$toolUtil.message(`${crossTips.value}`,'error')
							return false
						}else{
							context?.$http({
								url: `${tableName}/${!form.value.id ? "save" : "update"}`,
								method: 'post', 
								data: form.value 
							}).then(res=>{
								context?.$toolUtil.message(`操作成功`,'success',()=>{
									history.back()
								})
							})
						}
					})
				}else{
					context?.$http({
						url: `${tableName}/${!form.value.id ? "save" : "update"}`,
						method: 'post', 
						data: form.value 
					}).then(res=>{
						context?.$toolUtil.message(`操作成功`,'success',()=>{
							history.back()
						})
					})
				}
			}
		})
	}
	//修改跨表数据
	const changeCrossData=(row)=>{
		context?.$http({
			url: `${crossTable.value}/update`,
			method: 'post',
			data: row
		}).then(res=>{})
	}
	onMounted(()=>{
		type.value = route.query.type?route.query.type:'add'
		let row = null
		let table = null
		let statusColumnName = null
		let tips = null
		let statusColumnValue = null
		if(type.value == 'cross'){
			row = context?.$toolUtil.storageGet('crossObj')?JSON.parse(context?.$toolUtil.storageGet('crossObj')):{}
			table = context?.$toolUtil.storageGet('crossTable')
			statusColumnName = context?.$toolUtil.storageGet('crossStatusColumnName')
			tips = context?.$toolUtil.storageGet('crossTips')
			statusColumnValue = context?.$toolUtil.storageGet('crossStatusColumnValue')
		}
		init(route.query.id?route.query.id:null, type.value,'', row, table, statusColumnName, tips, statusColumnValue)
	})
	
</script>
<style lang="scss" scoped>
	// 面包屑盒子
	.bread_view {
		border: 1px solid #ddd;
		border-radius: 0px;
		padding: 12px 20px;
		margin: 10px auto 10px;
		background: #f9f9f9;
		width: 100%;
		position: relative;
		:deep(.breadcrumb) {
			font-size: 14px;
			line-height: 1;
			.el-breadcrumb__separator {
				margin: 0 9px;
				color: #ccc;
				font-weight: 500;
			}
			.first_breadcrumb {
				.el-breadcrumb__inner {
					color: #333;
					display: inline-block;
				}
			}
			.second_breadcrumb {
				.el-breadcrumb__inner {
					color: #999;
					display: inline-block;
				}
			}
		}
	}
	// 表单
	.add_form{
		border: 1px solid #0058ab20;
		border-radius: 0px;
		padding: 30px 7%;
		background: #0058ab02;
		// form item
		:deep(.el-form-item) {
			border: 0px solid #eee;
			padding: 0 0 20px;
			margin: 0 0 20px 0;
			background: none;
			display: flex;
			border-color: #0058ab20;
			border-width: 0 0 1px;
			border-style: dashed;
			//label
			.el-form-item__label {
			 color: inherit;
			 background: none;
			 font-weight: 500;
			 display: block;
			 width: auto;
			 font-size: inherit;
			 text-align: right;
			 min-width: 150px;
			}
			// 内容盒子
			.el-form-item__content {
				display: flex;
				width: calc(100% - 150px);
				justify-content: flex-start;
				align-items: center;
				flex-wrap: wrap;
				// 输入框
				.list_inp {
					border: 1px solid #0058ab30;
					padding: 0 10px;
					width: auto;
					line-height: 36px;
					box-sizing: border-box;
					min-width: 350px;
					height: 36px;
					//去掉默认样式
					.el-input__wrapper{
						border: none;
						box-shadow: none;
						background: none;
						border-radius: 0;
						height: 100%;
						padding: 0;
					}
					.is-focus {
						box-shadow: none !important;
					}
				}
				//日期选择器
				.list_date {
					border: 1px solid #0058ab30;
					border-radius: 0;
					background: #fff;
					width: auto;
					line-height: 36px;
					box-sizing: border-box;
					min-width: 300px;
					//去掉默认样式
					.el-input__wrapper{
						border: none;
						box-shadow: none;
						background: none;
						border-radius: 0;
						height: 100%;
					}
				}
			}
		}
	}
	// 按钮盒子
	.formModel_btn_box {
		padding: 0 150px;
		margin: 0 0 20px;
		display: flex;
		width: 100%;
		align-items: center;
		.formModel_cancel {
			border: 0;
			cursor: pointer;
			border-radius: 0px;
			padding: 0 24px;
			margin: 0 20px 0 0;
			outline: none;
			color: #333;
			background: #0058ab10;
			width: auto;
			font-size: 14px;
			height: 36px;
		}
		.formModel_cancel:hover {
		}
		
		.formModel_confirm {
			border: 0;
			cursor: pointer;
			border-radius: 0px;
			padding: 0 24px;
			margin: 0 20px 0 0;
			outline: none;
			color: #333;
			background: #0058ab30;
			width: auto;
			font-size: 14px;
			height: 36px;
		}
		.formModel_confirm:hover {
		}
	}
</style>